﻿using System.Collections.Generic;
using Inovout.Business;
using System.Windows;
using Inovout.Descriptors;
using Inovout.Portal.Desktop.Workbenchs;

namespace Inovout.Portal.Desktop.Controls
{
    public class RibbonGroupViewModel
    {
        private RibbonTabViewModel tab;
        public RibbonTabViewModel Tab
        {
            get { return tab; }
        }
        internal BaseWorkbenchViewModel WorkbenchViewModel
        {
            get { return tab.WorkbenchViewModel; }
        }
       private FunctionGroup functionGroup;
       public RibbonGroupViewModel(RibbonTabViewModel tab, FunctionGroup functionGroup)
        {
            this.tab = tab;
            this.functionGroup = functionGroup;
        }

        public string Header
        {
            get 
            {
                ClassDescriptor functionGroupClassDescriptor = null;

                if (functionGroup.GetType().Equals(typeof(FunctionGroup)))
                {
                    functionGroupClassDescriptor = ClassDescriptor.Create(functionGroup.Parent.GetType());
                    return functionGroupClassDescriptor.GetField(functionGroup.Name).Alias;
                }
                else
                {
                    functionGroupClassDescriptor = ClassDescriptor.Create(functionGroup.GetType());
                    return functionGroupClassDescriptor.Alias;
                }
            }
        }

        public Visibility Visibility
        {
            get
            {
                return Commands.Count == 0 ? Visibility.Collapsed : Visibility.Visible;
            }
        }
        private Function[] functions;
        internal Function[] Functions
        {
            get
            {
                if (functions == null)
                {
                    List<Function> functionList = new List<Function>();
                    foreach (var command in Commands)
                    {
                        functionList.Add(command.Function);
                    }
                    functions = functionList.ToArray();
                }
                return functions;
            }
        }
        private List<RibbonCommandViewModel> commands;
        public List<RibbonCommandViewModel> Commands
        {
            get
            {
                if (commands == null)
                {
                    commands = new List<RibbonCommandViewModel>();

                    foreach (IFunction funtion in functionGroup.Functions)
                    {
                        commands.Add(new RibbonCommandViewModel(this, funtion as Function));
                    }
                }
                return commands;
            }
            set { commands = value; }
        }
    }
}
